package com.kele.base.core;

import com.kele.base.BasePage;
import com.kele.base.bo.BaseBO;
import com.kele.base.entity.BaseEntity;
import com.kele.base.response.Result;
import com.kele.base.validate.ValidateGroup;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Set;

public abstract class BaseController<ENTITY extends BaseEntity<ID>, ID extends Serializable, BO extends BaseBO<ID>, VO> {

    private final BaseService<ENTITY, ID, BO, VO> service;

    public BaseController(BaseService<ENTITY, ID, BO, VO> service) {
        this.service = service;
    }

    /**
     * 根据id获取单个信息
     * @param id 主键
     * @return
     */
    @Operation(summary = "根据id获取单个信息")
    @GetMapping("/{id:\\d+}")
    public Result<VO> getById(@PathVariable("id") ID id){
        VO result = service.getVoById(id);
        return Result.ok(result);
    }

    @Operation(summary = "分页查询VO信息")
    @GetMapping("/page")
    public Result<Page<VO>> getPage(BO bo, BasePage pageQuery){
        Page<VO> pageVO = service.getPageVO(bo, pageQuery);
        return Result.ok(pageVO);
    }

    /**
     * 新增
     * @param param
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping()
    public Result<ID> add(@Validated(ValidateGroup.Insert.class) @RequestBody BO param){
        ID add = service.add(param);
        return Result.ok(add);
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @Operation(summary = "修改")
    @PutMapping()
    public Result<ID> edit(@Validated(ValidateGroup.Update.class) @RequestBody BO param){
        ID id = service.edit(param);
        return Result.ok(id);
    }

    /**
     * 根据id删除单个信息
     * @param id
     * @return
     */
    @Operation(summary = "根据id删除单个信息")
    @DeleteMapping("/{id:\\d+}")
    public Result<Void> delete(@PathVariable("id") ID id){
        service.delete(id);
        return Result.ok();
    }

    /**
     * 根据id批量删除信息
     * @param idList
     * @return
     */
    @Operation(summary = "根据id批量删除信息")
    @DeleteMapping("/batch")
    public Result<Void> deleteBatch(@RequestBody Set<ID> idList){
        service.delete(idList);
        return Result.ok();
    }
}
